﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Collections;

namespace DATA
{
   public  class PanelREP
    {
        MODELGENERALDataContext context = new MODELGENERALDataContext();
 
        public List<PanelDTO> GetAllActiveMainPanels()
        {
            List<PanelDTO> acc = new List<PanelDTO>();
            try 
            {	        
                var acounts = from a in context.Panels
                              where a.isActived == true && a.parentsID == null
                              orderby a.sortNumber
                              select a;
                foreach (var account in acounts)
                {
                    PanelDTO pan = new PanelDTO();
                    pan.Description = account.description;
                    pan.IsActived = account.isActived;
                    pan.Name = account.name;
                    pan.PanelID = account.panelID;
                    pan.ParentsID = null;
                    pan.PositionID = account.positionID;
                    pan.PositionName = GetPositionName(pan.PositionID);
                    pan.SortNumber = account.sortNumber;
                    acc.Add(pan);
                }
	        }
	        catch (Exception)
	        {}
             return acc;
        }

        public List<PanelDTO> GetAllActiveSubPanels()
        {
            List<PanelDTO> acc = new List<PanelDTO>();
            try
            {
                var acounts = from a in context.Panels
                              where a.isActived == true && a.positionID == null
                              orderby a.sortNumber
                              select a;
                foreach (var account in acounts)
                {
                    PanelDTO pan = new PanelDTO();
                    pan.Description = account.description;
                    pan.IsActived = account.isActived;
                    pan.Name = account.name;
                    pan.PanelID = account.panelID;
                    pan.ParentsID = account.parentsID;
                    pan.PositionID = null;
                    pan.ParentsName = GetParentsName(account.parentsID);
                    pan.SortNumber = account.sortNumber;
                    acc.Add(pan);
                }
            }
            catch (Exception)
            { }
            return acc;
        }

        public List<PanelDTO> GetActiveMainPanels(int positionID)
        {
            List<PanelDTO> acc = new List<PanelDTO>();

            var acounts = from a in context.Panels
                          where a.isActived == true && a.parentsID==null && a.positionID == positionID
                          orderby a.sortNumber
                          select a;
            foreach (var account in acounts)
            {
                PanelDTO pan = new PanelDTO();
                pan.Description = account.description;
                pan.IsActived = account.isActived;
                pan.Name = account.name;
                pan.PanelID = account.panelID;
                pan.ParentsID = null;
                pan.PositionID = account.positionID;
                pan.PositionName = GetPositionName(pan.PositionID);
                pan.SortNumber = account.sortNumber;
                acc.Add(pan);
            }
            return acc;
        }

        public List<PanelDTO> GetActiveSubPanels(int? mainPanelID)
        {
            List<PanelDTO> acc = new List<PanelDTO>();

            var acounts = from a in context.Panels
                          where a.isActived == true && a.parentsID == mainPanelID
                          orderby a.sortNumber
                          select a;
            foreach (var account in acounts)
            {
                PanelDTO pan = new PanelDTO();
                pan.Description = account.description;
                pan.IsActived = account.isActived;
                pan.Name = account.name;
                pan.PanelID = account.panelID;
                pan.ParentsID = account.parentsID;
                pan.ParentsName = GetParentsName(pan.ParentsID);
                pan.PositionID = null;
                pan.SortNumber = account.sortNumber;
                acc.Add(pan);
            }
            return acc;
        }

        private string GetParentsName(int? id)
        {
            var a = context.Panels.Single(s => s.panelID == id);
            return a.name;
        }

        private string GetPositionName(int? id)
        {
            var a = context.Positions.Single(s => s.positionID == id);
            return a.positionName;
        }

        public bool AddPanel(PanelDTO account)
        {
            try
            {
                Panel pan = new Panel();
                pan.description = account.Description;
                pan.isActived = account.IsActived;
                pan.name = account.Name;
                pan.panelID = account.PanelID;
                pan.parentsID = account.ParentsID;
                pan.positionID = account.PositionID;
                pan.sortNumber = account.SortNumber;
                context.Panels.InsertOnSubmit(pan);
                context.SubmitChanges();
            }
            catch (Exception)
            {
                return false;
            }
            return true;

        }
        public void DeletePanel(int AccountID)
        {
            var acc = context.Panels.Single(a => a.panelID == AccountID);
            context.Panels.DeleteOnSubmit(acc);
            context.SubmitChanges();


        }

        public bool UpdatePanel(PanelDTO account)
        {
            try
            {
                Panel pan = context.Panels.Single( nv => nv.panelID == account.PanelID);
                pan.description = account.Description;
                pan.isActived = account.IsActived;
                pan.name = account.Name;
                pan.panelID = account.PanelID;
                pan.parentsID = account.ParentsID;
                pan.positionID = account.PositionID;
                pan.sortNumber = account.SortNumber;
                context.SubmitChanges();
            }

            catch (System.Exception ex)
            {
                return false;
            }
            return true;
        }

        public void ReOrder(ArrayList l)
        {
            try
            {
                for (int i = 0; i < l.Count; i++)
                {
                    int ii = (int)l[i];
                    var p = context.Panels.Single(a => a.panelID == ii);
                    p.sortNumber = i+1;
                }
                context.SubmitChanges();
            }
            catch (Exception)
            {}
        }
    }
}
